package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.domain.ErpMaterialCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ErpMaterialTypeMapper;
import com.ruoyi.domain.ErpMaterialType;
import com.ruoyi.service.IErpMaterialTypeService;

/**
 * 物料类型Service业务层处理
 * 
 * @author zhp
 * @date 2025-07-29
 */
@Service
public class ErpMaterialTypeServiceImpl implements IErpMaterialTypeService
{
    @Autowired
    private ErpMaterialTypeMapper erpMaterialTypeMapper;

    /**
     * 查询物料类型
     * 
     * @param id 物料类型主键
     * @return 物料类型
     */
    @Override
    public ErpMaterialType selectErpMaterialTypeById(Integer id)
    {
        return erpMaterialTypeMapper.selectErpMaterialTypeById(id);
    }

    /**
     * 查询物料类型列表
     * 
     * @param erpMaterialType 物料类型
     * @return 物料类型
     */
    @Override
    public List<ErpMaterialType> selectErpMaterialTypeList(ErpMaterialType erpMaterialType)
    {
        return erpMaterialTypeMapper.selectErpMaterialTypeList(erpMaterialType);
    }

    /**
     * 新增物料类型
     * 
     * @param erpMaterialType 物料类型
     * @return 结果
     */
    @Override
    public int insertErpMaterialType(ErpMaterialType erpMaterialType)
    {
        return erpMaterialTypeMapper.insertErpMaterialType(erpMaterialType);
    }

    /**
     * 修改物料类型
     * 
     * @param erpMaterialType 物料类型
     * @return 结果
     */
    @Override
    public int updateErpMaterialType(ErpMaterialType erpMaterialType)
    {
        return erpMaterialTypeMapper.updateErpMaterialType(erpMaterialType);
    }

    /**
     * 批量删除物料类型
     * 
     * @param ids 需要删除的物料类型主键
     * @return 结果
     */
    @Override
    public int deleteErpMaterialTypeByIds(Integer[] ids)
    {
        return erpMaterialTypeMapper.deleteErpMaterialTypeByIds(ids);
    }

    /**
     * 删除物料类型信息
     * 
     * @param id 物料类型主键
     * @return 结果
     */
    @Override
    public int deleteErpMaterialTypeById(Integer id)
    {
        return erpMaterialTypeMapper.deleteErpMaterialTypeById(id);
    }

    @Override
    public List<Map<String, Object>> getTypeTreeForCascader() {
        // 1. 获取所有启用的分类
        List<ErpMaterialType> allCategories = erpMaterialTypeMapper.selectTypeTreeForCascader();

        // 2. 构建树形结构
        return buildTree(allCategories);
    }

    private List<Map<String, Object>> buildTree(List<ErpMaterialType> tupes) {

        // 创建ID到节点的映射
        Map<Long, Map<String, Object>> nodeMap = new HashMap<>();
        tupes.forEach(c -> {
            Map<String, Object> node = new HashMap<>();
            node.put("value", c.getId());
            node.put("label", c.getName());
            node.put("children", new ArrayList<>());
            nodeMap.put(c.getId(), node);
        });

        // 构建树
        List<Map<String, Object>> tree = new ArrayList<>();
        tupes.forEach(c -> {
            Map<String, Object> node = nodeMap.get(c.getId());
            if (c.getParentId() == null || c.getParentId() == 0) {
                tree.add(node);
            } else {
                Map<String, Object> parent = nodeMap.get(c.getParentId());
                if (parent != null) {
                    ((List)parent.get("children")).add(node);
                }
            }
        });
        return tree;
    }

}
